package com.hmall.order.listener;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hmall.common.feign.ItemClient;
import com.hmall.order.constant.MqConstant;
import com.hmall.order.mapper.OrderDetailMapper;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class OrderListener {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private ItemClient itemClient;
    @RabbitListener(queues = MqConstant.DELAY_QUEUE)
    public void listenItemOnSale(String id){
        Order order = orderMapper.selectById(Long.valueOf(id));
        if (order == null){
            return;
        }
        Integer status = order.getStatus();
        if (status.equals(1)){
            order.setStatus(5);
            orderMapper.updateById(order);
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("order_id",order.getId());
            OrderDetail orderDetail = orderDetailMapper.selectOne(queryWrapper);
            itemClient.updateStock(orderDetail.getItemId(), -orderDetail.getNum());


        }
    }

}
